#include "DialogComponents.h"
#include "GlobalComponents.h"
#include "CommonFunc.h"
#include <QIcon>

BaseDialog::BaseDialog(QWidget * parent)
           : QDialog(parent)
{
    // 去掉窗口的问号
    Qt::WindowFlags flags = Qt::Dialog;
    flags |= Qt::WindowCloseButtonHint;
    setWindowFlags(flags);

    m_pQVBoxLayout = new QVBoxLayout(this);
    setLayout(m_pQVBoxLayout);
    m_pLayoutFunc = new QHBoxLayout;

    m_pQVBoxLayout->setSpacing(1);
    m_pQVBoxLayout->setContentsMargins(5, 0, 5, 5);
}

// 确认按钮触发
void BaseDialog::slt_clicked_Done()
{
    if(true == funcDone())
    {
        close();
    }
}

// 取消按钮
void BaseDialog::slt_clicked_Cancel()
{
    funcCancel();
    close();
}

// 添加布局控件到主体窗口
void BaseDialog::addWidget2BaseLayout(QWidget *pWidget, Qt::Alignment alignment)
{
    m_pQVBoxLayout->addWidget(pWidget, alignment);
}

// 界面添加操作控件
void BaseDialog::addLayout2BaseLayout(QLayout *layout)
{
    m_pQVBoxLayout->addLayout(layout);
}

// 添加控制按钮到底部工具栏
void BaseDialog::addWidget2FuncLayout(QWidget *pWidget, bool bAddStretch)
{
    if(nullptr != pWidget)
    {
        m_pLayoutFunc->addWidget(pWidget);
        if(true == bAddStretch)
        {
            m_pLayoutFunc->addStretch();
        }
    }
}

// 取消按钮
void BaseDialog::addBaseFuncAction()
{
    addFuncStretch();   // 工具栏添加间隔
    addFuncCancel();    // 添加取消按钮
    addFuncDone();      // 添加确认按钮
    addFuncLayout();    // 将工具栏添加到界面中
}

// [子类选配]添加底部工具栏
void BaseDialog::addFuncLayout()
{
    if(nullptr != m_pLayoutFunc)
    {
        m_pQVBoxLayout->addLayout(m_pLayoutFunc, Qt::AlignBottom);
    }
}

// [子类选配]添加完成按钮
void BaseDialog::addFuncDone()
{
    QPushButton* pOK = NewPTBWithIcon(QStringLiteral("确认"), QStringLiteral("确认输入数据"), QIcon(QString(":/operationIcons/resource/operationIcons/check_yes.ico")), this);
    pOK->setMaximumWidth(80);
    connect(pOK, SIGNAL(clicked()), this, SLOT(slt_clicked_Done()));
    m_pLayoutFunc->addWidget(pOK);
}

// [子类选配]添加取消按钮
void BaseDialog::addFuncCancel()
{
    QPushButton* pCancel = NewPTBWithIcon(QStringLiteral("取消"), QStringLiteral("取消本输入框"), QIcon(QString(":/operationIcons/resource/operationIcons/close.ico")), this);
    pCancel->setMaximumWidth(80);
    connect(pCancel, SIGNAL(clicked()), this, SLOT(slt_clicked_Cancel()));
    m_pLayoutFunc->addWidget(pCancel);
}

// [子类选配]工具栏添加间隔
void BaseDialog::addFuncStretch()
{
    if(nullptr != m_pLayoutFunc)
    {
        m_pLayoutFunc->addStretch();
    }
}

// 数据输入窗口初始化
DialogInput::DialogInput(QWidget * parent)
    : BaseDialog(parent)
    , m_pQTextEdit(nullptr)
{
    // 设置窗口标题和限制窗口大小
    setWindowIcon(QIcon(":/operationIcons/resource/operationIcons/information_succeed.ico"));
    setWindowTitle(QString("参数输入"));
    setFixedSize(600, 400);

    m_pQTextEdit = new QTextEdit(this);
    addWidget2BaseLayout(m_pQTextEdit, Qt::AlignTop);
    m_pQTextEdit->setMaximumHeight(450);
    m_pQTextEdit->setPlaceholderText(QStringLiteral("输入待解析JSON文本"));

    addBaseFuncAction();
}

// 完成按钮功能
bool DialogInput::funcDone()
{
    QString strContext = m_pQTextEdit->document()->toPlainText();
    if(!strContext.isEmpty())
    {
        emit sendInputData(strContext);
    }

    m_pQTextEdit->clear();

    return true;
}

// 完成取消功能
void DialogInput::funcCancel()
{
    m_pQTextEdit->clear();
}

// 数据输出窗口初始化
DialogOutput::DialogOutput(QWidget * parent)
    : BaseDialog(parent)
    , m_pQTextBrowser(nullptr)
{
    // 设置窗口标题和限制窗口大小
    setWindowIcon(QIcon(":/operationIcons/resource/operationIcons/information_succeed.ico"));
    setWindowTitle(QString("参数展示"));
    setFixedSize(700, 500);

    m_pQTextBrowser = new QTextBrowser(this);
    addWidget2BaseLayout(m_pQTextBrowser, Qt::AlignTop);
    m_pQTextBrowser->setMaximumHeight(450);
    m_pQTextBrowser->setPlaceholderText(QStringLiteral("待输出文本为空"));
    m_pQTextBrowser->setFont(gstFont);
    m_pQTextBrowser->setTabStopWidth(20);   // 设置缩进20个像素

    QHBoxLayout* pLayout = new QHBoxLayout;
    QPushButton* pCopy2Clipboard = NewPTBWithIcon(QStringLiteral("复制"), QStringLiteral("复制展示文本到剪贴板"), QIcon(QString(":/operationIcons/resource/operationIcons/file_copy.ico")), this);
    connect(pCopy2Clipboard, SIGNAL(clicked()), this, SLOT(slt_Copy2Clipboard()));
    pLayout->addStretch();
    pLayout->addWidget(pCopy2Clipboard);
    pCopy2Clipboard->setMaximumWidth(100);

    addLayout2BaseLayout(pLayout);
}

// 输入待展示数据并弹出窗口
void DialogOutput::inputData(const QString &strShow)
{
    m_pQTextBrowser->clear();
    m_pQTextBrowser->setText(strShow);

    exec();
}

// 拷贝数据到剪贴板
void DialogOutput::slt_Copy2Clipboard()
{
    QString strLastData = m_pQTextBrowser->document()->toPlainText();
    if(strLastData.isEmpty())
    {
        myMessageBoxShowOne(FORM_NULL, MSG_SHOW_ERROR, QString("当前文本内容为空，无数据复制到粘贴板!"));
        return;
    }

    QApplication::clipboard()->setText(strLastData);
    QString strShow = QString("[%1] 成功将结果复制到粘贴板，数据长度 %2 byte").arg(GetCurrentTime(SHOWTIME_TYPE_hhmmss), QString::number(strLastData.length()));
    myMessageBoxShowOne(FORM_NULL, MSG_SHOW_INFO, strShow);
}
